//
//  EXAbstractPersistentObject.m
//  ORM
//
//  Created by PH on 7.1.10.
//  Copyright 2010 Codesign. All rights reserved.
//

#import "EXAbstractPersistentObject.h"
#import "EXObjectStore.h"

@implementation EXAbstractPersistentObject

- (void)__setObjectStore:(EXObjectStore*)_objectStore {
	__objectStore = _objectStore;
}

- (EXObjectStore*)__objectStore {
	return __objectStore;
}

/*- (void)release {
	BOOL discarded = __objectStore != nil && [self retainCount] == 2;
	[super release];
	if (discarded) {
		[__objectStore discardObject: self];
	}
}*/

- (void)dealloc {
	//NSAssert(__objectStore != nil, @"Object store of an object being dealloc is nil");
	//if (__objectStore == nil) NSLog(@"Object store of an object being dealloc is nil: %@", [self className]);
	//else NSLog(@"discarding object");
	[__objectStore discardObject: self];
	[super dealloc];
}

- (void)finalize {
	[__objectStore discardObjectGC: self];
	[super finalize];
}

- (id)initWithCoder:(NSCoder*)coder {
	if ((self = [super init])) {
		for (NSString* name in [[self class] ORMPersistentFieldNames]) {
			id value = [coder decodeObjectForKey: name];
			[self setValue: value forKey: name];
		}
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder*)coder {
	for (NSString* name in [[self class] ORMPersistentFieldNames]) {
		[coder encodeObject: [self valueForKey: name] forKey: name];
	}
}

@end
